package com.appcheck.recording.monitors;

import java.util.TimerTask;

import com.appcheck.recording.listener.impl.OrientationDetector;
import com.appcheck.recording.utils.Logger;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.OrientationEventListener;

final public class ActivityChangedTask extends TimerTask {

	private ActivityChangedMonitor activityChangedMonitor;
	
	ActivityChangedTask(ActivityChangedMonitor activityChangedMonitor){
		this.activityChangedMonitor = activityChangedMonitor;
	}
	@Override
	public void run() {
		//getLastActivity()返回的是当前的activity
	    Activity lastActivity = activityChangedMonitor.getActivityMonitor().getLastActivity();
	    //activityChangedMonitor.getActivity()返回的是保存的上一个acitivity
	    //横竖屏切换也会触发两者不相等
	    if (activityChangedMonitor.getActivity() != lastActivity){
	    	if(activityChangedMonitor.getActivity() == null){
	    		//一开始的情况
	    		activityChangedMonitor.setmOrientationDetector(new OrientationDetector(lastActivity));
	    		activityChangedMonitor.getmOrientationDetector().enable();
	    	}
	    	Logger.INFO(new StringBuilder("Activity changed: ").append(lastActivity.getLocalClassName()).toString());
	    	
	    	activityChangedMonitor.setActivity(lastActivity);
	    	Intent intent = new Intent("AddMsgToQueue").putExtra("msg", "1");
	    	
	    	activityChangedMonitor.getInstrumentation().getContext().sendBroadcast(intent);
	    	
	    }
	}

}
